package com.foxnicweb.app.relation.modules;



import com.foxnicweb.app.constants.db.PatentTables;
import com.foxnicweb.app.constants.enums.patent.ProcessRoleType;
import com.foxnicweb.app.domain.patent.PatentFile;
import com.foxnicweb.app.domain.patent.PatentMember;
import com.foxnicweb.app.domain.patent.PatentProcess;
import com.foxnicweb.app.domain.patent.PatentProcessRole;
import com.foxnicweb.app.domain.patent.meta.PatentProcessMeta;
import com.foxnicweb.app.domain.patent.meta.PatentProcessRoleMeta;
import com.github.foxnic.commons.collection.CollectorUtil;
import com.github.foxnic.dao.relation.RelationManager;
import org.github.foxnic.web.constants.db.FoxnicWeb;
import org.github.foxnic.web.domain.bpm.meta.CatalogMeta;

import java.util.List;
import java.util.Map;

public class PatentRelationManager extends RelationManager {
    @Override
    protected void config() {
        this.setupRelations();


    }




    private void setupRelations() {


        this.property(PatentProcessMeta.PATENT_PROP)
                .using(PatentTables.PAT_PATENT_PROCESS.ID).join(PatentTables.PAT_PATENT.ID);


        this.property(PatentProcessMeta.MEMBER_LIST_PROP)
                .using(PatentTables.PAT_PATENT_PROCESS.ID).join(PatentTables.PAT_PATENT_MEMBER.PATENT_ID)
                .after((tag,owner,data,map)->{
                    Map<String, List<PatentMember>> group= CollectorUtil.groupBy(data,PatentMember::getMemberType);
                    owner.setMemberMap(group);
                    return data;
                });

        this.property(PatentProcessMeta.FILE_LIST_PROP)
                .using(PatentTables.PAT_PATENT_PROCESS.ID).join(PatentTables.PAT_PATENT_FILE.PATENT_ID)
                .after((tag,owner,data,map)->{
                    Map<String, List<PatentFile>> group= CollectorUtil.groupBy(data,PatentFile::getNodeId);
                    owner.setFileMap(group);
                    return data;
                });



        this.property(PatentProcessMeta.CASE_AGENT_LIST_PROP)
                .using(PatentTables.PAT_PATENT_PROCESS.ID).join(PatentTables.PAT_PATENT_PROCESS_ROLE.PATENT_PROCESS_ID)
                .conditionEquals(PatentTables.PAT_PATENT_PROCESS_ROLE.ROLE_TYPE, ProcessRoleType.CASE_AGENT.code())
        ;

        this.property(PatentProcessMeta.OUT_PERSON_LIST_PROP)
                .using(PatentTables.PAT_PATENT_PROCESS.ID).join(PatentTables.PAT_PATENT_PROCESS_ROLE.PATENT_PROCESS_ID)
                .conditionEquals(PatentTables.PAT_PATENT_PROCESS_ROLE.ROLE_TYPE, ProcessRoleType.OUT_PERSON.code())
        ;

        this.property(PatentProcessMeta.BUSINESS_ASSISTANT_LIST_PROP)
                .using(PatentTables.PAT_PATENT_PROCESS.ID).join(PatentTables.PAT_PATENT_PROCESS_ROLE.PATENT_PROCESS_ID)
                .conditionEquals(PatentTables.PAT_PATENT_PROCESS_ROLE.ROLE_TYPE, ProcessRoleType.BUSINESS_ASSISTANT.code())
        ;

        this.property(PatentProcessRoleMeta.EMPLOYEE_PROP)
                .using(PatentTables.PAT_PATENT_PROCESS_ROLE.EMPLOYEE_ID).join(FoxnicWeb.HRM_EMPLOYEE.ID);

    }



}
